package org.opensuse.bs.ui.actions;

import java.util.Vector;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.opensuse.bs.ui.BSUIPlugin;
import org.opensuse.bs.ui.OSC;
import org.opensuse.bs.ui.model.RootElement;
import org.opensuse.bs.ui.views.RemoteView;

public class SearchAction extends Action {
	private TreeViewer viewer;
	private RemoteView view;
	
	private class SearchDialog extends Dialog {
		private Text projectName;
		private Text packageName;
		private int whole;

		public SearchDialog(Shell parentShell) {
			super(parentShell);
			whole = 0;
		}
		
		protected Control createDialogArea(Composite parent) {
			Composite composite = (Composite)super.createDialogArea(parent);
			GridLayout layout = new GridLayout();
			composite.setLayout(layout);
			layout.numColumns = 2;
			Label label1 = new Label(composite, SWT.NONE);
			label1.setText("Project");
			GridData gd = new GridData(GridData.GRAB_HORIZONTAL);
			projectName = new Text(composite, SWT.BORDER);
			projectName.setLayoutData(gd);
			gd.horizontalAlignment = SWT.FILL;
			gd.widthHint = 200;
			Label label2 = new Label(composite, SWT.NONE);
			label2.setText("Package");
			gd = new GridData(GridData.GRAB_HORIZONTAL);
			packageName = new Text(composite, SWT.BORDER);
			packageName.setLayoutData(gd);
			gd.horizontalAlignment = SWT.FILL;
			gd.widthHint = 200;
			Button b = new Button(composite, SWT.CHECK);
			b.setText("Whole World");
			b.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent e) {				
				}

				public void widgetSelected(SelectionEvent e) {
					whole = 1-whole;
				}
			});
			return composite;
		}
		
		public void createButtonsForButtonBar(Composite parent) {
			GridLayout layout = new GridLayout();
			parent.setLayout(layout);
			layout.numColumns = 2;
			
			GridData gd = new GridData();
			Button button1 = new Button(parent, SWT.PUSH);
			button1.setText("find");
			button1.setLayoutData(gd);
			gd.widthHint = 100;
			button1.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent e) {				
				}

				public void widgetSelected(SelectionEvent e) {
					final String prj = projectName.getText().trim();
					final String pac = packageName.getText().trim();
					if(pac.length() != 0) {
						Job findPac = new Job("finding "+pac) {
							protected IStatus run(IProgressMonitor monitor) {
								String cmd = "search --package "+pac;
								if(whole == 1)cmd = cmd+" -e";
								Vector<String> result = OSC.run(cmd);
								if(result.isEmpty()) {
									return Status.CANCEL_STATUS;
								}
								String head = result.elementAt(0);
								if(head.startsWith("matches")) {
									BSUIPlugin plugin = BSUIPlugin.getDefault();
									Vector<String> aim = plugin.getAimed();
									if(!aim.isEmpty())
										aim.clear();
									for(int i=2;i<result.size();i++) {
										String line = result.elementAt(i);
										int space = line.lastIndexOf(" ");
										aim.add(line.substring(space+1));
									}
									if(monitor.isCanceled())
										return Status.CANCEL_STATUS;
									Display display = BSUIPlugin.getDefault().getWorkbench().getDisplay();
									display.asyncExec(new Runnable() {
										public void run() {
											viewer.setInput(new RootElement(2));
											view.getAddToWatchListAction().setEnabled(true);
										}
									});
								}
								return Status.OK_STATUS;
							}
						};
						findPac.schedule();
					}else if(prj.length() != 0) {
						Job findPrj = new Job("finding "+prj) {
							protected IStatus run(IProgressMonitor monitor) {
								String cmd = "search --project "+prj;
								if(whole == 1)cmd = cmd+" -e";
								Vector<String> result = OSC.run(cmd);
								if(result.isEmpty()) {
									return Status.CANCEL_STATUS;
								}
								String head = result.elementAt(0);
								if(head.startsWith("matches")) {
									BSUIPlugin plugin = BSUIPlugin.getDefault();
									Vector<String> aim = plugin.getAimed();
									if(!aim.isEmpty())
										aim.clear();
									for(int i=2;i<result.size();i++) {
										aim.add(result.elementAt(i));
									}
									if(monitor.isCanceled())
										return Status.CANCEL_STATUS;
									Display display = BSUIPlugin.getDefault().getWorkbench().getDisplay();
									display.asyncExec(new Runnable() {
										public void run() {
											viewer.setInput(new RootElement(2));
										}
									});
								}
								return Status.OK_STATUS;
							}
						};
						findPrj.schedule();
					}
				}
			});
			
			gd = new GridData();
			Button button2 = new Button(parent, SWT.PUSH);
			button2.setText("close");
			button2.setLayoutData(gd);
			gd.widthHint = 100;
			button2.addSelectionListener(new SelectionListener() {
				public void widgetDefaultSelected(SelectionEvent e) {				
				}

				public void widgetSelected(SelectionEvent e) {
					SearchDialog.this.close();
				}
			});
		}
	}
	
	public SearchAction(TreeViewer viewer, RemoteView view) {
		super("Search");
		this.viewer = viewer;
		this.view = view;
	}

	public void run() {
		SearchDialog dialog = new SearchDialog(viewer.getControl().getShell());
		dialog.setBlockOnOpen(false);
		dialog.open();
	}
}
